package com.eachonline.smart.web.course;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.oss.OssConstant;
import com.eachonline.oss.OssService;
import com.eachonline.smart.mapper.SmartSupportRelateMapper;
import com.eachonline.smart.model.SmartClassTeacher;
import com.eachonline.smart.model.SmartClassTeacherExample;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartCourseExample;
import com.eachonline.smart.model.SmartCourseTimeList;
import com.eachonline.smart.model.SmartCourseTimeListExample;
import com.eachonline.smart.model.SmartOrderRecord;
import com.eachonline.smart.model.SmartOrderRecordExample;
import com.eachonline.smart.model.SmartSupportRelate;
import com.eachonline.smart.model.SmartSupportRelateExample;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.model.SmartUserCourse;
import com.eachonline.smart.model.SmartUserCourseExample;
import com.eachonline.smart.model.base.BaseSmartCourseExample.Criteria;
import com.eachonline.smart.service.ClassTeacherService;
import com.eachonline.smart.service.CourseService;
import com.eachonline.smart.service.CourseTimeService;
import com.eachonline.smart.service.OrderRecordService;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.smart.service.UserCourseService;
import com.eachonline.smart.util.OrderStatus;
import com.eachonline.smart.web.common.CommonUtil;
import com.eachonline.util.DateUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/course")
public class CourseController{
	
	@Resource
	private CourseService courseService;
	
	@Resource
	private UserCourseService userCourseService;
	
	@Resource
	private SmartUserService userService;
	
	@Resource
	private CourseTimeService courseTimeService;

	@Resource
	private OssService ossService;
	
	@Resource
	private OrderRecordService orderRecordService;
	
	@Resource
	private ClassTeacherService cassTeacherService;
	
	
	@Resource
	private SmartSupportRelateMapper supportRelateMapper;
	
	
	/**
	 * 查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryCourseList",method = RequestMethod.POST)
	public JsonResult queryHomeCourseList(HttpServletRequest request,String site,String courseType,String courseLevel,String courseCategory,String courseName,String courseRank,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");

		
	
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		Pagination<SmartCourse> coursePageInfo= courseService.queryCourseList(begin, pageSize, site, courseType, courseLevel, courseCategory,courseName,courseRank);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("courseInfo", coursePageInfo);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	
	/**
	 * 查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryCourseListBySort",method = RequestMethod.POST)
	public JsonResult queryCourseListBySort(HttpServletRequest request,String site,String courseType,String courseLevel,String courseCategory,String courseName,String courseRank,String sort,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
	
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		Pagination<SmartCourse> coursePageInfo= courseService.queryCourseListBySort(begin, pageSize, site, courseType, courseLevel, courseCategory, courseName, courseRank, sort);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("courseInfo", coursePageInfo);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	
	
	
	/**
	 * 管理系统查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manger/queryCourseList",method = RequestMethod.POST)
	public JsonResult queryMangerCourseList(HttpServletRequest request,String status,String courseType,String courseLevel,String courseCategory,String courseName,String courseRank,HttpServletResponse  response,String userName,String site){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");

	
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		Pagination<SmartCourse> coursePageInfo= courseService.queryMangerCourseList(begin, pageSize, status, courseType, courseLevel, courseCategory,courseName,courseRank,userName,site);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("courseInfo", coursePageInfo);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	
	
	/**
	 * 管理课程详情
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manger/queryCourseDetail",method = RequestMethod.POST)
	public JsonResult queryMangerCourseDetail(HttpServletRequest request,String courseId,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");

		SmartCourse course=courseService.selectByPrimaryKey(courseId);
		
		if(course==null)
		{
			return new JsonResult(false, "该课程不存在");
		}
		SmartUser user=userService.selectByPrimaryKey(course.getUserSid());
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("course", course);
		resultMap.put("smartUser", user);
		return new JsonResult(true, "成功", resultMap);
	}
	
	
	/**
	 * 课程点赞
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/supportCourse",method = RequestMethod.POST)
	public JsonResult supportCourse(HttpServletRequest request,String courseId,String userId,int su,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");

		if(su==1)
		{
			  SmartSupportRelateExample  sre=new SmartSupportRelateExample();
			  sre.createCriteria().andBusinessIdEqualTo(courseId).andUserIdEqualTo(userId);
			  List<SmartSupportRelate>  list= supportRelateMapper.selectByExample(sre);
			  if(!list.isEmpty())
			  {
				  return new JsonResult(false, "该课程已经点赞过，不能重复点赞");
			  }
			
            SmartCourse course=courseService.selectByPrimaryKey(courseId);
			
			if(course==null)
			{
				return new JsonResult(false, "该课程不存在");
			}
			int supportNum=course.getSupportNum()+su;
			
			if(supportNum<=0)
			{
				supportNum=0;
			}
			course.setSupportNum(supportNum);	
			courseService.updateByPrimaryKeySelective(course);
			
			SmartSupportRelate support=new SmartSupportRelate();
			
			support.setSid(UUIDUtil.getUUID());
			support.setUserId(userId);
			support.setBusinessId(courseId);
			supportRelateMapper.insert(support);
			return new JsonResult(true, "成功");
		}
		
		if(su==-1)
		{

            SmartCourse course=courseService.selectByPrimaryKey(courseId);
			
			if(course==null)
			{
				return new JsonResult(false, "该课程不存在");
			}
			int supportNum=course.getSupportNum()+su;
			
			if(supportNum<=0)
			{
				supportNum=0;
			}
			course.setSupportNum(supportNum);	
			courseService.updateByPrimaryKeySelective(course);
			SmartSupportRelateExample  sre=new SmartSupportRelateExample();
			 sre.createCriteria().andBusinessIdEqualTo(courseId).andUserIdEqualTo(userId);
			supportRelateMapper.deleteByExample(sre);
			
		}
		
		return new JsonResult(false, "参数错误");
	}
	
	
	
	/**
	 *  课程审核
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manger/updateCourse",method = RequestMethod.POST)
	public JsonResult updateMangerCourseStatus(HttpServletRequest request,String courseId,String status,String comment,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		
		
		SmartCourse course= courseService.selectByPrimaryKey(courseId);
		if(course==null)
		{
			return new JsonResult(false, "该课程不存在");
		}
		
		course.setCheckStatus(status);
		course.setComment(comment);
		
		
		if("2"==status||"2".equalsIgnoreCase(status))
		{
			
			try { 
				SmartClassTeacherExample scte=new SmartClassTeacherExample();
				scte.createCriteria().andUserIdEqualTo(course.getUserSid()).andCourseIdEqualTo(courseId);
				
			   List<SmartClassTeacher>	teacherList= cassTeacherService.selectByExample(scte);
			   if(teacherList.isEmpty())
			   {
					cassTeacherService.createClassByCourse(courseId);
			   }
			   
			   SmartOrderRecordExample  ore=new SmartOrderRecordExample();
			   ore.createCriteria().andBusinessIdEqualTo(courseId).andOrderStatusEqualTo(OrderStatus.WAIT);
			   List<SmartOrderRecord> orderList=  orderRecordService.selectByExample(ore);
			   
			   if(!orderList.isEmpty())
			   {
				   for (SmartOrderRecord  order: orderList) {
					   order.setBusinessFee(course.getCoursePrice());
					   orderRecordService.updateByPrimaryKeySelective(order);  
				}
			   }
				   
			   
			   
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				 return new JsonResult(false, e.getMessage());
			}
		}
		
		courseService.updateByPrimaryKeySelective(course);
		
		 return new JsonResult(true, "修改成功");
	}


	/**
	 *  课程是否置顶
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manger/updateCourseSite")
	public JsonResult updateMangerCourseStatus(HttpServletRequest request,String courseId,String site,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		
		SmartCourse course= courseService.selectByPrimaryKey(courseId);
		if(course==null)
		{
			return new JsonResult(false, "该课程不存在");
		}
		
		course.setCourseSite(site);
		courseService.updateByPrimaryKeySelective(course);
		
		 return new JsonResult(true, "修改成功");
	}
	

	/**
	 * 查询发布者未开始列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryTeacherNoCourse",method = RequestMethod.POST)
	public JsonResult queryTeacherNOCourseList(HttpServletRequest request,String teacherId,String courseType,String courseLevel,String courseCategoryCode,String courseName,String courseRank,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		
		int begin =pageSize*(start-1);
		SmartCourseExample  sce=new SmartCourseExample();
		Criteria  cri=sce.createCriteria();
		if(StringUtil.isEmpty(teacherId))
		{
			return new JsonResult(false, "老师Id不能为空");
		}
		cri.andUserSidEqualTo(teacherId);
		if(StringUtil.isNotEmpty(courseType))
		{
			cri.andCourseTypeEqualTo(courseType);
		}
		
		if(StringUtil.isNotEmpty(courseCategoryCode))
		{
			cri.andCourseCategroyCodeLike("%"+courseCategoryCode+"%");
		}
		
		if(StringUtil.isNotEmpty(courseName))
		{
			cri.andCourseNameLike("%"+courseName+"%");
		}
		
		if(StringUtil.isNotEmpty(courseRank))
		{
			cri.andCourseRankEqualTo(courseRank);
		}
		
		Date  nowDate=DateUtil.getNowDate();
		cri.andCourseStartTimeGreaterThan(nowDate);
		Pagination<SmartCourse>  coursePage=courseService.queryByPage(sce, start, pageSize, "create_time desc");
		Map<String,Object> dateMap=new HashMap<String, Object>();
		dateMap.put("courseNoStartPage", coursePage);
		return new JsonResult(true, "成功",coursePage);
		
		
	}
	
	
	/**
	 * 查询发布者正在开始列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryTeacherIngCourse",method = RequestMethod.POST)
	public JsonResult queryTeacherIngCourse(HttpServletRequest request,String teacherId,String courseType,String courseLevel,String courseCategoryCode,String courseName,String courseRank,HttpServletResponse  response){
	
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		
		int begin =pageSize*(start-1);
		SmartCourseExample  sce=new SmartCourseExample();
		Criteria  cri=sce.createCriteria();
		if(StringUtil.isEmpty(teacherId))
		{
			return new JsonResult(false, "老师Id不能为空");
		}
		cri.andUserSidEqualTo(teacherId);
		if(StringUtil.isNotEmpty(courseType))
		{
			cri.andCourseTypeEqualTo(courseType);
		}
		
		if(StringUtil.isNotEmpty(courseCategoryCode))
		{
			cri.andCourseCategroyCodeLike("%"+courseCategoryCode+"%");
		}
		
		if(StringUtil.isNotEmpty(courseName))
		{
			cri.andCourseNameLike("%"+courseName+"%");
		}
		
		if(StringUtil.isNotEmpty(courseRank))
		{
			cri.andCourseRankEqualTo(courseRank);
		}
		
		Date  nowDate=DateUtil.getNowDate();
		cri.andCourseStartTimeLessThanOrEqualTo(nowDate);		
		cri.andCourseEndTimeGreaterThanOrEqualTo(nowDate);
		Pagination<SmartCourse>  coursePage=courseService.queryByPage(sce, start, pageSize, "create_time desc");
		Map<String,Object> dateMap=new HashMap<String, Object>();
		dateMap.put("courseIngPage", coursePage);
		return new JsonResult(true, "成功",coursePage);
		
		
	}
	
	
	
	/**
	 * 查询发布者已结束列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryTeacherEndCourse",method = RequestMethod.POST)
	public JsonResult queryTeacherEdCourse(HttpServletRequest request,String teacherId,String courseType,String courseLevel,String courseCategoryCode,String courseName,String courseRank,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		
		int begin =pageSize*(start-1);
		SmartCourseExample  sce=new SmartCourseExample();
		Criteria  cri=sce.createCriteria();
		if(StringUtil.isEmpty(teacherId))
		{
			return new JsonResult(false, "老师Id不能为空");
		}
		cri.andUserSidEqualTo(teacherId);
		if(StringUtil.isNotEmpty(courseType))
		{
			cri.andCourseTypeEqualTo(courseType);
		}
		
		if(StringUtil.isNotEmpty(courseCategoryCode))
		{
			cri.andCourseCategroyCodeLike("%"+courseCategoryCode+"%");
		}
		
		if(StringUtil.isNotEmpty(courseName))
		{
			cri.andCourseNameLike("%"+courseName+"%");
		}
		
		if(StringUtil.isNotEmpty(courseRank))
		{
			cri.andCourseRankEqualTo(courseRank);
		}
		
		Date  nowDate=DateUtil.getNowDate();
		cri.andCourseEndTimeLessThan(nowDate);
		Pagination<SmartCourse>  coursePage=courseService.queryByPage(sce, start, pageSize, "create_time desc");
		Map<String,Object> dateMap=new HashMap<String, Object>();
		dateMap.put("courseEndPage", coursePage);
		return new JsonResult(true, "成功",coursePage);
		
		
	}
	/**
	 * 查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryTeacherCourseList",method = RequestMethod.POST)
	public JsonResult queryTeacherCourseList(HttpServletRequest request,String teacherId,String courseType,String courseLevel,String courseCategory,String courseName,String courseRank,HttpServletResponse  response){
		
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		Pagination<SmartCourse> coursePageInfo= courseService.queryTeacherCourseList(begin, pageSize, teacherId, courseType, courseLevel, courseCategory,courseName, courseRank);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("teacherCoursePage", coursePageInfo);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	
	
	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public JsonResult saveEntity(HttpServletRequest request,SmartCourse entity,String startTimeStr,String endTimeStr,String courseTimeListString){
		
		SmartUser currentUser=CommonUtil.getCurrentUser(request);
		
		if(currentUser==null)
		{
			JsonResult jsonResult = new JsonResult(false, "登录失效,请先登录之后再发布课程！");
			jsonResult.setCode("5001");
			return jsonResult;
		}
	
	   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
	   try {
		entity.setCourseStartTime(sdf.parse(startTimeStr));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return new JsonResult(false, "课程开始时间格式错误");
		}
		
	   try {
			entity.setCourseEndTime(sdf.parse(endTimeStr));
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return new JsonResult(false, "课程结束时间格式错误");
			}
	   
	   if(entity.getCourseEndTime().compareTo(entity.getCourseStartTime())<=0)
	   {
		   return new JsonResult(false, "开始时间必须等于 结束时间");
	   }
	   
		try {
			if(StringUtil.isEmpty(entity.getSid()))
			{
				entity.setCheckStatus("1");
				entity.setSupportNum(0);
				entity.setStudyNum("0");
				entity.setCourseSite("0");
				entity.setSid(UUIDUtil.getUUID());
				entity.setUserSid(currentUser.getSid());
				entity.setTeacherId(currentUser.getSid());
				entity.setTeacherName(currentUser.getUserName());
				entity.setCreateTime(new Date());
				entity.setModifyTime(new Date());
				courseService.insert(entity);
			}else
			{
				entity.setCourseSite("0");
				entity.setCreateTime(new Date());
				entity.setCheckStatus("1");
				entity.setModifyTime(new Date());
				courseService.updateByPrimaryKeySelective(entity);
			}
		
		List<SmartCourseTimeList> courseList=new ArrayList<SmartCourseTimeList>();
		
		if(StringUtil.isNotEmpty(courseTimeListString))
		{
			try {
				 courseList=JSON.parseArray(courseTimeListString, SmartCourseTimeList.class);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				return new JsonResult(false, "课时格式错误");
			}
			int i=0;
			for (SmartCourseTimeList courseTime:courseList) {
					courseTime.setCreateTime(new Date());
					try {
						courseTime.setCourseStartTime(DateUtil.parseDate(courseTime.getCourseStartDateStr()+" "+courseTime.getCourseStartTimeStr()));
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						courseTime.setCourseEndTime(DateUtil.parseDate(courseTime.getCourseEndDateStr()+" "+courseTime.getCourseEndTimeStr()));
						
					} catch (Exception e) {
						// TODO: handle exception
					}
					courseTime.setCourseId(entity.getSid());
				if(StringUtil.isEmpty(courseTime.getSid()))
				{
					courseTime.setSid(UUIDUtil.getUUID());
					courseTime.setCourseSort(i);
					courseTimeService.insert(courseTime);
				}else{
					courseTime.setCourseSort(i);
					courseTimeService.updateByPrimaryKeySelective(courseTime);
				}
				i=i+1;
			}
			
		}
	
			
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	
	
	
	/**
	 * 删除banner图片接口
	 * @param pickey
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteBannerImg",method = RequestMethod.POST)
	public JsonResult deleteBannerImg(String pickey){
		
		ossService.delete(OssConstant.NAMESPACE_BUSINESS,pickey);
		return new JsonResult(true, "删除成功!");
	}
	
	/**
	 * 编辑
	 */
	@ResponseBody
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public JsonResult updateEntity(SmartCourse entity) {
		try {
			courseService.updateByPrimaryKeySelective(entity);
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	
	/**
	 * 删除课时接口
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteCourseTime",method = RequestMethod.POST)
	public JsonResult deleteCourseTime(String  courseTimeId) {
		try {
           if(StringUtil.isNotEmpty(courseTimeId))
           {
   			courseTimeService.deleteByPrimaryKey(courseTimeId);

           }
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	/**
	 * 查询
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryCourseDetail",method = RequestMethod.POST)
	public JsonResult queryEntity(String sid,String userId){
		try {
			Map<String,Object> resultMap=new HashMap<String, Object>();
			
		     SmartCourse course=courseService.selectByPrimaryKey(sid);
		     SmartCourseTimeListExample ctEax=new SmartCourseTimeListExample();
		     ctEax.createCriteria().andCourseIdEqualTo(sid) ;
		     Pagination<SmartCourseTimeList> pageInfo=courseTimeService.queryByPage(ctEax, 0, 50, " course_sort asc ");
		     List<SmartCourseTimeList>  courseTimeList =pageInfo.getDataList();
		     List<SmartSupportRelate> list= new ArrayList<SmartSupportRelate>();
		     List<SmartUserCourse> orderList= new ArrayList<SmartUserCourse>();
		    if(StringUtil.isNotEmpty(userId))
		    {
		    	  SmartSupportRelateExample  sre=new SmartSupportRelateExample();
				  sre.createCriteria().andBusinessIdEqualTo(sid).andUserIdEqualTo(userId);
				  list= supportRelateMapper.selectByExample(sre);
				  
				  SmartUserCourseExample ore=new SmartUserCourseExample();
				  ore.createCriteria().andCourseIdEqualTo(sid).andUserIdEqualTo(userId);
				  
				  orderList= userCourseService.selectByExample(ore);
				
				  if(userId==course.getUserSid()||userId.equals(course.getUserSid()))
					{
					  resultMap.put("isSelfCourse",1);
					}else
					{
					  resultMap.put("isSelfCourse",0);
					}
		    }else
		    {
		    	resultMap.put("isSelfCourse",0);
		    }
			
		    
		  
		    if((course.getCourseEndTime().compareTo(new Date()))<0)
		    {
		    	 resultMap.put("courseOver",1);
		    }else
		    {
		    	 resultMap.put("courseOver",0);
		    }
		    
		    if(list.isEmpty())
			  {
				  resultMap.put("courseSupport", 0);
			  }else
			  {
				  resultMap.put("courseSupport",1);
			  }
		    
		    if(orderList.isEmpty())
			  {
				  resultMap.put("isBuy", 0);
			  }else
			  {
				  resultMap.put("isBuy",1);
			  }
		    resultMap.put("course", course);
		    resultMap.put("courseTimeList", courseTimeList);

			return new JsonResult(true,resultMap);
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public JsonResult deleteEntities(String sid){
		try {
			courseService.deleteByPrimaryKey(sid);
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}

	
}
